/**
 * ekanya
 */
var Ekanya = {
    id: "EkanyaTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Ekanya.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '病历号', field: 'caseNo', align: 'center', valign: 'middle', sortable: true, width: 100,},
        {
            title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return "<a href='javascript:;' onclick='Ekanya.consultantToDetail(" + '"' + row.id + '"' + ")'>" + row.customerName + "</a>"
            },
        },
        {title: '门诊', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true, width: 110,},
        {title: '电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 110,},
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 60},
        {title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true, width: 60,},
        {title: '创建时间', field: 'filingTime', align: 'center', valign: 'middle', sortable: true, width: 95,},
        {title: '收费时间', field: 'getTime', align: 'center', valign: 'middle', sortable: true, width: 95,},
        {title: '客户类型', field: 'customerStatus', align: 'center', valign: 'middle', sortable: true, width: 60},
        {title: '医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true, width: 60},
        {title: '咨询师', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true, width: 60},
        {title: '网店咨询', field: 'archiving', align: 'center', valign: 'middle', sortable: true, width: 60},
        {title: '收费项目', field: 'payServiceName', align: 'center', valign: 'middle', sortable: true, width: 60},
        {title: '应收', field: 'foldingPay', align: 'center', valign: 'middle', sortable: true, width: 80},
        {title: '实收', field: 'serviceActualPay', align: 'center', valign: 'middle', sortable: true, width: 80},
        {title: '实际应收', field: 'shijiPay', align: 'center', valign: 'middle', sortable: true, width: 80},
        {title: '欠费', field: 'arrears', align: 'center', valign: 'middle', sortable: true, width: 80},
        {
            title: '美团点评', field: 'meituan', visible: false, align: 'center', valign: 'middle',

        },
        {
            title: '电子扫码', field: 'dzsm', align: 'center', valign: 'middle', sortable: true, width: 90,
        },
        {title: '现金', field: 'cash', visible: false, align: 'center', valign: 'middle'},
        {title: '银行卡', field: 'brankCard', visible: false, align: 'center', valign: 'middle'},
        {
            title: '积分抵扣', field: 'jifen', align: 'center', valign: 'middle', sortable: true, width: 80,
        },
        {
            title: '社保卡', field: 'sbk', align: 'center', valign: 'middle', sortable: true, width: 80,
        },
        {title: '华荣时代', field: 'hrsd', align: 'center', valign: 'middle', sortable: true, width: 80},
        {
            title: '跟踪状态', field: 'followStatus', align: 'center', valign: 'middle', sortable: true, width: 100,
        },
        {
            title: '天猫', field: 'tianmao', align: 'center', valign: 'middle', sortable: true, width: 100,
        },
        {
            title: 'VIP收费', field: 'vip', align: 'center', valign: 'middle', sortable: true, width: 95,
        },
        {
            title: '双11抵扣金', field: 'ssyi', align: 'center', valign: 'middle', sortable: true, width: 100,
        },
        {
            title: '百度钱包', field: 'baidu', align: 'center', valign: 'middle', sortable: true, width: 95,
        },
        {
            title: '优惠券', field: 'yhq', align: 'center', valign: 'middle', sortable: true, width: 95,
        },
    ]
    return columns;
};


/**
 * 检查是否选中
 */
Ekanya.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Ekanya.seItem = selected[0];
        return true;
    }
};

Ekanya.inquire = function () {
    Ekanya.search()
}
/**
 * 查询预约客户列表
 */
Ekanya.search = function () {
    Ekanya.table.refresh({query: Ekanya.formParams()});
};

var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}
//把对象拼接成URL的键值对参数param1=value1&param2=value2
var parseParam = function (param) {
    var paramStr = "";

    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};

var getSingleData = function (param, key) {
    return key + "=" + param[key];
}

Ekanya.resetSearch = function () {
    $("#reach").val("");
    $("#doctor").val("");
    $("#caseNo").val("");
    $("#reach").val("");
    $("#fieldTc").val("");
    document.getElementById("startGetTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endGetTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    Ekanya.search();
};



/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
Ekanya.formParams = function () {
    var queryData = {};
    queryData['reach'] = $("#reach").val();
    queryData['startGetTime'] = $("#startGetTime").val();
    queryData['endGetTime'] = $("#endGetTime").val();
    queryData['fieldTc'] = $("#fieldTc").val();
    queryData['doctor'] = $("#doctor").val();
    return queryData;
}
$(function () {
    $.ajax({
        url: Feng.ctxPath + "/consultant/getDoctor",
        type: "get",
        success: function (data) {
            var txt = '<option value="">--请选择医生--</option>';
            for (var i = 0; i < data.length; i++) {
                txt += '<option value="' + data[i].doctor + '">' + data[i].doctor + '</option>';
            }
            document.getElementById('doctor').innerHTML = txt;
            //状态单选
            var seleteDoctorValue = $('#doctorValue').val();
            if (seleteDoctorValue != "") {
                $("#doctor option[value=" + seleteDoctorValue + "]").attr("selected", "selected");
            }
        },
        error: function (data) {
            alert('内部故障!');
        }
    });
    document.getElementById("startGetTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endGetTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    var defaultColunms = Ekanya.initColumn();
    var table = new BSTable(Ekanya.id, "/ekanya/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(Ekanya.formParams);
    Ekanya.table = table.init();
    Ekanya.search();

    //初始化下拉经济能力列表
    $.post(Feng.ctxPath + "/tc/getList", function (data) {
        var txt = '<option value="">--请选择现场TC--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].tcName + '">' + data[i].tcName + '</option>';
        }
        document.getElementById('fieldTc').innerHTML = txt;
        //状态单选
        var seletefieldTc = $('#fieldTcValue').val();
        if (seletefieldTc != "") {
            $("#fieldTc option[value=" + seletefieldTc + "]").attr("selected", "selected");
        }
    }, "json");

    //采用定时器监听 时间内容是否变化
    var startGetTimeTwo = $("#startGetTime").val();
    var endGetTimeTwo = $("#endGetTime").val();

    setInterval(
        function () {//定时执行方法内容
            if ($("#startGetTime").val() != startGetTimeTwo || $("#endGetTime").val() != endGetTimeTwo) {//如果时间发生变化，就触发查询事件
                Ekanya.search()
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                startGetTimeTwo = $("#startGetTime").val();
                endGetTimeTwo = $("#endGetTime").val();
            }

        }, 500);
});
